package uy.edu.um.ltic.server.communication;

import java.rmi.RemoteException;
import java.sql.Timestamp;
import java.util.ArrayList;

import uy.edu.um.ltic.commons.EmployeeVO;
import uy.edu.um.ltic.commons.MessageRemoteMgt;
import uy.edu.um.ltic.commons.MessageVO;
import uy.edu.um.ltic.exception.DataBaseException;
import uy.edu.um.ltic.server.business.ConferenceMessage;
import uy.edu.um.ltic.server.business.Employee;
import uy.edu.um.ltic.server.business.FileMessage;
import uy.edu.um.ltic.server.business.Message;
import uy.edu.um.ltic.server.business.MessageFactory;
import uy.edu.um.ltic.server.business.TextMessage;
public class MessageRemoteServerMgr implements MessageRemoteMgt {

	private static MessageRemoteServerMgr instance = null;

	private MessageRemoteServerMgr() throws RemoteException {

	}

	public static MessageRemoteServerMgr getInstance() throws RemoteException {
		if (instance == null) {
			instance = new MessageRemoteServerMgr();
		}
		return instance;
	}

	public void addMessage(MessageVO oMessage) throws RemoteException, DataBaseException {
		if (oMessage.getType() == 1) { // 1:TextMessage
			Employee oSend = new Employee(oMessage.getSender().getnEmployee());
			ArrayList<Employee> oALReci = new ArrayList<Employee>();
			for (EmployeeVO eVO : oMessage.getReceivers()) {
				Employee oReci = new Employee(eVO.getnEmployee());
				oALReci.add(oReci);
			}
			Message tM = new TextMessage(oMessage.getDate(), oALReci, oSend,
					oMessage.getText()); // ACA
			MessageFactory.getInstance().getMessageMgt().addMessage(tM);
		} else if (oMessage.getType() == 2) { // 2:ConferenceMessage
			Employee oSend = new Employee(oMessage.getSender().getnEmployee());
			ConferenceMessage cM = new ConferenceMessage(oMessage.getDate(),
					null, oSend, oMessage.getText(), oMessage.getIdConference());
			MessageFactory.getInstance().getMessageMgt().addMessage(cM);
		} else if (oMessage.getType() == 3) { // 3:FileMessage
			Employee oSend = new Employee(oMessage.getSender().getnEmployee());
			ArrayList<Employee> oALReci = new ArrayList<Employee>();
			for (EmployeeVO eVO : oMessage.getReceivers()) {
				Employee oReci = new Employee(eVO.getnEmployee());
				oALReci.add(oReci);
			}
			if (oMessage.getReceivers() != null) {
				FileMessage fM = new FileMessage(oMessage.getData(),
						oMessage.getName(), oMessage.getDate(), oALReci, oSend);
				MessageFactory.getInstance().getMessageMgt()
						.addMessage(fM);
				MessageVO oMess = new MessageVO(new Timestamp(System.currentTimeMillis()),oMessage.getReceivers(),oSend.convertToEmployeeVO()," le ha enviado el archivo: " + oMessage.getName() + ".",null,null,1,0);
				addMessage(oMess);
			}  
		}

	}

	public ArrayList<MessageVO> getMessagesBetweenTwo(EmployeeVO sender,
			EmployeeVO receiver) throws RemoteException, DataBaseException {

		Employee oReci = new Employee(receiver.getnEmployee());
		Employee oSender = new Employee(sender.getnEmployee());
		ArrayList<Message> oList = MessageFactory.getInstance()
				.getMessageMgt().getMessagesBetweenTwo(oSender, oReci);
		ArrayList<MessageVO> oListVO = new ArrayList<MessageVO>();

		for (Message oMsg : oList) {

			ArrayList<EmployeeVO> oALReciVO = new ArrayList<EmployeeVO>();
			for (Employee empl : oMsg.getReceivers()) {
				EmployeeVO oReciVO = new EmployeeVO(empl.getName(),
						empl.getSurname()); // //solo nombre
				oALReciVO.add(oReciVO);
			}
			MessageVO oMsgVO = null;
			if (oMsg instanceof TextMessage) {
				TextMessage tM = (TextMessage) oMsg;
				oMsgVO = new MessageVO(tM.getDate(), oALReciVO, tM.getSender()
						.convertToEmployeeVO(), tM.getText(), null, null, 1,
						null);
			}
			oListVO.add(oMsgVO);
		}

		return oListVO;
	}

	public ArrayList<MessageVO> getConferenceMessages(int idConference) throws RemoteException, DataBaseException{
		ArrayList<MessageVO> oListMess = new ArrayList<MessageVO>();
		for (ConferenceMessage confMess : MessageFactory.getInstance()
				.getMessageMgt().getConferenceMessages(idConference)) {
			oListMess.add(confMess.convertToMessageVO());
		}
		return oListMess;
	}

	@Override
	public ArrayList<EmployeeVO> getLastEmployees(int nEmployee) throws RemoteException, DataBaseException{

		ArrayList<Employee> lEmpl = MessageFactory.getInstance()
				.getMessageMgt().getLastEmployees(nEmployee);
		ArrayList<EmployeeVO> lEmplVO = new ArrayList<EmployeeVO>();
		for (Employee emp : lEmpl) {
			lEmplVO.add(emp.convertToEmployeeVO());
		}
		return lEmplVO;
	}

	@Override
	public ArrayList<MessageVO> getMessagesWithTheWord(String word) throws RemoteException, DataBaseException{
		ArrayList<TextMessage> oLiMe = MessageFactory.getInstance().getMessageMgt().getMessagesWithTheWord(word);
		ArrayList<MessageVO> oLiMeVO = new ArrayList<MessageVO>();
		for (TextMessage tM : oLiMe){
			oLiMeVO.add(tM.convertToMessageVO());
		}
		return oLiMeVO;
	}

	@Override
	public int getAmountOfWordsWritten(int nEmployee, Timestamp date1,
			Timestamp date2) throws RemoteException, DataBaseException{
		return MessageFactory.getInstance().getMessageMgt().getAmountOfWordsWritten(nEmployee, date1, date2);
	}

	@Override
	public ArrayList<Integer> getFilesFromOneEmployeeToAnother(int sender,
			int receiver) throws RemoteException, DataBaseException {
		return  MessageFactory.getInstance().getMessageMgt().getFilesFromOneEmployeeToAnother(sender, receiver);
	}

	@Override
	public String getNameOfFile(int id) throws RemoteException, DataBaseException {
		return  MessageFactory.getInstance().getMessageMgt().getNameOfFile(id);
	}

	@Override
	public byte[] getFile(int id) throws RemoteException, DataBaseException {
		return  MessageFactory.getInstance().getMessageMgt().getFile(id);
	}

	
	
	
	// @Override PREGUNTAR A IVAN!!!
	// public void createALLTablesMessages() throws RemoteException{
	// TODO Auto-generated method stub

	// }

}
